public class CommentRemover {
    public static String removeComments(String code) {
        return code.replaceAll(
                "/\\*([\\s\\S]*?)\\*/|//.*?(?=\\n|$)",
                ""
        ).replaceAll("\\n+", "\n").trim();
    }

    // 删除注释与换行
    public static void main(String[] args) {
        String code = "//fdsebdfbndx\n" +
                "class Solution {\n" +
                "    public int[] twoSum(int[] nums, int target) {\n" +
                "        Map<Integer, Integer> hash = new HashMap<>();\n" +
                "        for (int i = 0; i < nums.length; i++) {// ew2gvwegv\n" +
                "            if (hash.containsKey(target - nums[i])) {\n" +
                "                return new int[] { hash.get(target - nums[i]), i };\n" +
                "\n" +
                "                \n" +
                "            } else {\n" +
                "                hash.put(nums[i], i);\n" +
                "            }\n" +
                "        }\n" +
                "\n" +
                "        \n" +
                "        return new int[] { -1, -1 };\n" +
                "    }\n" +
                "}\n" +
                "\n" +
                "// 而不哇hnsr\n" +
                "/**\n" +
                " * dsbsaerfb\n" +
                " */";
        String codeWithoutComments = removeComments(code);
        System.out.println(codeWithoutComments);
    }
}